#include "StdAfx.h"
#include "OutputData.h"

using namespace std;
const string number2string(const vector<double>& in)
{
	FTOA f;
	vector<string> vs;
	vs.clear();
	string s("");
	std::transform(in.begin(), in.end(),
		std::back_inserter(vs), f);
	for (vector<string>::iterator head = vs.begin(); head != vs.end(); ++head)
	{
		s+= *head;
		if (head != (--vs.end()) )
		{
			s+= ',';
			s+= ' ';
		}
	}
	return s;
}

ofstream& open_file(ofstream& file, const string& name)
{
	file.close();
	file.clear();
	file.open(name.c_str());
	return file;
}

bool numbers2file(ofstream& file, const string& comment, const vector<double>& iv)
{
	file << comment << endl;
	vector<double>::const_iterator iter = iv.begin();
	for (; iter != iv.end(); ++iter)
	{
		file << *iter << endl;
	}
	return file.good();
}

OutputData::OutputData():m_root(0)
{
	TiXmlBase::SetCondenseWhiteSpace(true);
}
OutputData::OutputData(const string& infile):m_root(0)
{
	BaseFile(infile.c_str());
	TiXmlBase::SetCondenseWhiteSpace(true);

}

OutputData::~OutputData(void)
{
	//if (m_root)
	//	delete m_root;
}

bool OutputData::BaseFile(const string& infile)
{
	return m_doc.LoadFile(infile.c_str());
}
bool OutputData::SaveFile(const string& name)
{
	TiXmlElement* m_root = new TiXmlElement("output");
	MessageMap::iterator iter;
	TiXmlElement* msg;
	TiXmlText* text;
	for (iter=m_map.begin(); iter != m_map.end(); iter++)
	{
		const string & key=(*iter).first;
		const string & value=(*iter).second;
		msg = new TiXmlElement(key.c_str());
		text = new TiXmlText(value.c_str());
		msg->LinkEndChild(text);
		m_root->LinkEndChild(msg);
	}
	m_doc.LinkEndChild(m_root);
	return m_doc.SaveFile(name.c_str());
}
// write runoff during calibration to the file
bool OutputData::RunoffCalibration(const vector<double>& vr)
{
	/*ofstream ofile("RunoffDuringCalibration.txt");
	ofile.clear();
	bool bs = numbers2file(ofile, "Runoff During Calibration", vr);*/
	dump("RunoffDuringCalibration.txt", vr);
	string s = number2string(vr);
	m_map["RunoffDuringCalibration"] = s;
	return true;
}

bool OutputData::RunoffValidation(const vector<double>& vr)
{
	/*ofstream ofile("RunoffDuringValidation.txt");
	ofile.clear();
	bool bs = numbers2file(ofile, "Runoff During Validation", vr);*/
	dump("RunoffDuringValidation.txt", vr);
	string s = number2string(vr);
	m_map["RunoffDuringValidation"] = s;
	return true;
}
bool OutputData::RunoffSimulation(const vector<double>& vr)
{
	dump("RunoffDuringSimulation.txt", vr);
	string s = number2string(vr);
	m_map["RunoffDuringSimulation"] = s;
	return true;
}
// write the NCUC model parameters to the xml file
bool OutputData::ModelParameters(const vector<double>& vr)
{	
	//dump("RunoffDuringSimulation.txt", vr);
	string s = number2string(vr);
	m_map["ModelParameters"] = s;
	return true;
}
// write CE value of the calibration to the xml file
bool OutputData::CECalibration(const double& ce)
{
	using boost::lexical_cast;
	string s = lexical_cast<string> (ce);
	m_map["CEDuringCalibration"] = s;
	return true;
}
// write CE value of the validation to the xml file
bool OutputData::CEValidation(const double& ce)
{
	using boost::lexical_cast;
	string s = lexical_cast<string> (ce);
	m_map["CEDuringValidation"] = s;
	return true;
}

// write CE value of the simulation to the xml file
bool OutputData::CESimulation(const double& ce)
{
	using boost::lexical_cast;
	string s = lexical_cast<string> (ce);
	m_map["CEDuringSimulation"] = s;
	return true;
}
void OutputData::dump(const string& name, const vector<double>& vr)
{
	ofstream ofile(name.c_str());
	ofile.clear();
	ofile.precision(10);
	numbers2file(ofile, name, vr);
}